/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package com.wisnu.scs.dao.impl;

import com.wisnu.scs.dao.DbBarangMasukDao;
import com.wisnu.scs.entity.DbBarang;
import com.wisnu.scs.entity.DbBarangMasuk;
import com.wisnu.scs.error.ErrorMessage;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.ListIterator;
import org.hibernate.HibernateException;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.Transaction;

public class DbBarangMasukDaoImpl implements DbBarangMasukDao {

    private final SessionFactory sessionFactory;

    public DbBarangMasukDaoImpl(SessionFactory sessionFactory) {
        this.sessionFactory = sessionFactory;
    }

    @Override
    public void insertBarangMasuk(DbBarangMasuk barangMasuk, DbBarang barang) throws ErrorMessage {
        Transaction transaction = null;
        Session session = sessionFactory.openSession();
        try {
            transaction = session.beginTransaction();
            session.save(barangMasuk);
            session.update(barang);
            transaction.commit();
        } catch (HibernateException exception) {
            if (transaction != null) {
                transaction.rollback();
                throw new ErrorMessage(exception.getMessage());
            }
        } finally {
            session.flush();
            session.close();
        }
    }

    @Override
    public void updateBarangMasuk(DbBarangMasuk barangMasuk, DbBarang barang) throws ErrorMessage {
        Transaction transaction = null;
        Session session = sessionFactory.openSession();
        try {
            transaction = session.beginTransaction();
            session.update(barangMasuk);
            session.update(barang);
            transaction.commit();
        } catch (HibernateException exception) {
            if (transaction != null) {
                transaction.rollback();
                throw new ErrorMessage(exception.getMessage());
            }
        } finally {
            session.flush();
            session.close();
        }
    }

    @Override
    public void deleteBarangMasuk(DbBarangMasuk barangMasuk, DbBarang barang) throws ErrorMessage {
        Transaction transaction = null;
        Session session = sessionFactory.openSession();
        try {
            transaction = session.beginTransaction();
            session.delete(barangMasuk);
            session.update(barang);
            transaction.commit();
        } catch (HibernateException exception) {
            if (transaction != null) {
                transaction.rollback();
                throw new ErrorMessage(exception.getMessage());
            }
        } finally {
            session.flush();
            session.close();
        }
    }

    @Override
    public List<DbBarangMasuk> selectBarangMasukByDate(String SearchText, Date date) throws ErrorMessage {
        List<DbBarangMasuk> list = new ArrayList<>();
        Transaction transaction = null;
        Session session = sessionFactory.openSession();
        try {
            transaction = session.beginTransaction();
            list = session.createQuery("from DbBarangMasuk where TANGGAL_MASUK = :date")
                    .setDate("date", date).list();

            ListIterator<DbBarangMasuk> iterator = list.listIterator();
            while (iterator.hasNext()) {
                if (!iterator.next().getDbBarang().getNamaBarang().toLowerCase().contains(SearchText)) {
                    iterator.remove();
                }
            }

            transaction.commit();
        } catch (HibernateException exception) {
            if (transaction != null) {
                transaction.rollback();
                throw new ErrorMessage(exception.getMessage());
            }
        } finally {
            session.flush();
            session.close();
        }
        return list;
    }

    @Override
    public List<DbBarangMasuk> selectBarangMasukByDateRange(String SearchText, List<Date> dateList) throws ErrorMessage {
        List<DbBarangMasuk> list = new ArrayList<>();
        Transaction transaction = null;
        Session session = sessionFactory.openSession();
        try {
            transaction = session.beginTransaction();
            list = session.createQuery("from DbBarangMasuk where TANGGAL_MASUK >= :firstDate AND TANGGAL_MASUK <= :lastDate")
                    .setDate("firstDate", dateList.get(0))
                    .setDate("lastDate", dateList.get(1))
                    .list();
            ListIterator<DbBarangMasuk> iterator = list.listIterator();
            while (iterator.hasNext()) {
                if (!iterator.next().getDbBarang().getNamaBarang().toLowerCase().contains(SearchText)) {
                    iterator.remove();
                }
            }
            transaction.commit();
        } catch (HibernateException exception) {
            if (transaction != null) {
                transaction.rollback();
                throw new ErrorMessage(exception.getMessage());
            }
        } finally {
            session.flush();
            session.close();
        }
        return list;
    }

    @Override
    public List<DbBarangMasuk> selectAllBarangMasuk() throws ErrorMessage {
        List<DbBarangMasuk> list = new ArrayList<>();
        Transaction transaction = null;
        Session session = sessionFactory.openSession();
        try {
            transaction = session.beginTransaction();
            list = session.createQuery("from DbBarangMasuk").list();
            transaction.commit();
        } catch (HibernateException exception) {
            if (transaction != null) {
                transaction.rollback();
                throw new ErrorMessage(exception.getMessage());
            }
        } finally {
            session.flush();
            session.close();
        }
        return list;
    }

    @Override
    public List<DbBarangMasuk> selectBarangMasukByKeyWords(String SearchText) throws ErrorMessage {
        List<DbBarangMasuk> list = new ArrayList<>();
        Transaction transaction = null;
        Session session = sessionFactory.openSession();
        try {
            transaction = session.beginTransaction();
            list = session.createQuery("from DbBarangMasuk").list();

            ListIterator<DbBarangMasuk> iterator = list.listIterator();
            while (iterator.hasNext()) {
                if (!iterator.next().getDbBarang().getNamaBarang().toLowerCase().contains(SearchText)) {
                    iterator.remove();
                }
            }
            transaction.commit();
        } catch (HibernateException exception) {
            if (transaction != null) {
                transaction.rollback();
                throw new ErrorMessage(exception.getMessage());
            }
        } finally {
            session.flush();
            session.close();
        }
        return list;
    }

}
